package com.posdemo;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //订单菜品集合
    private List<FoodsBean> foodsBean;

    private Pos pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void test(View view)
    {
        switch (view.getId())
        {
            case R.id.btn_print:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // 开启一个子线程
                        new Thread() {
                            public void run() {
                                try {
                                    pos = new Pos("192.168.1.52", 9100, "GBK");  //第一个参数是打印机网口IP

                                    //初始化打印机
                                    pos.initPos();

                                    //初始化订单数据
                                    initData();

                                    pos.bold(true);
                                    pos.printTabSpace(2);
                                    pos.printWordSpace(1);
                                    pos.printLocation(1);
                                    pos.printText("**测试店铺");

                                    pos.printLocation(0);
                                    pos.printTextNewLine("-----------------------------");
                                    pos.bold(false);
                                    pos.printTextNewLine("订 单 号：1005199");
                                    pos.printTextNewLine("用 户 名：15712937281");
                                    pos.printTextNewLine("桌  号：3号桌");
                                    pos.printTextNewLine("订单状态：订单已确认");
                                    pos.printTextNewLine("订单日期：2017/7/26 12:34:53");
                                    pos.printTextNewLine("付 款 人：线下支付（服务员：宝哥）");
                                    pos.printTextNewLine("服 务 员：1001");
                                    pos.printTextNewLine("订单备注：不要辣，少盐");
                                    pos.printLine(2);
                                    pos.printText("品项");
                                    pos.printLocation(20, 1);
                                    pos.printText("单价");
                                    pos.printLocation(99, 1);
                                    pos.printWordSpace(1);
                                    pos.printText("数量");
                                    pos.printWordSpace(3);
                                    pos.printText("小计");
                                    pos.printTextNewLine("-----------------------------");

                                    for (FoodsBean foods : foodsBean) {
                                        pos.printTextNewLine(foods.getName());
                                        pos.printLocation(20, 1);
                                        pos.printText(foods.getPrice());
                                        pos.printLocation(99, 1);
                                        pos.printWordSpace(1);
                                        pos.printText(foods.getNumber());
                                        pos.printWordSpace(3);
                                        pos.printText(foods.getSum());
                                    }

                                    pos.printTextNewLine("-----------------------------");

                                    pos.printLocation(1);
                                    pos.printLine(2);


                                    //切纸
                                    pos.feedAndCut();

                                    pos.closeIOAndSocket();
                                    pos = null;
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        }.start();
                    }
                }).start();

                break;

            case R.id.QR:
                // 开启一个子线程
                new Thread() {
                    public void run() {
                        try {
                            pos = new Pos("192.168.1.52", 9100, "GBK");  //第一个参数是打印机网口IP
                            //初始化打印机
                            pos.initPos();

                            //打印二维码  注：有的pos 不支持打印机  这个就打印不了   但可以通过图片打印 来实现
                            pos.qrCode("http://blog.csdn.net");
                            pos.printTextNewLine("-----------------------------");
                            //切纸
                            pos.feedAndCut();
                            pos.closeIOAndSocket();
                            pos = null;
                        }
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                        }}.start();

                break;


            //条形码  打印
            case R.id.Barcode:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            pos = new Pos("192.168.1.52", 9100, "GBK");  //第一个参数是打印机网口IP
                            //初始化打印机
                            pos.initPos();
                            //空两行;
                            pos.printLine(2);
                            //打印条形码
                            pos.Barcode("1123456452");
                            //空两行
                            pos.printLine(2);
                            //切纸
                            pos.feedAndCut();
                            pos.closeIOAndSocket();
                            pos = null;

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                break;
            case R.id.bitmaptest:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            pos = new Pos("192.168.1.52", 9100, "GBK");  //第一个参数是打印机网口IP

                            //初始化打印机
                            pos.initPos();


                            pos.printLocation(1);
                            pos.printText("------------");
                            pos.printLine(2);

                            pos.addBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.qr));
                            //切纸


                            pos.printLine(2);
                            pos.printText("------------");
                            pos.printLine();
                            pos.feedAndCut();
                            pos.closeIOAndSocket();
                            pos = null;

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                break;
        }
    }
    private void initData() {
        foodsBean = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            FoodsBean fb = new FoodsBean();
            fb.setName("测试菜" + i);
            fb.setPrice("90.00");
            fb.setNumber("1" + i);
            fb.setSum("10" + i + ".00");
            foodsBean.add(fb);
        }
    }
}
